home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / PMACROS.H < prev    next >
Text File  |  1989-08-17  |  7KB  |  377 lines

  1. ;Assembly language macros for C compiler independence.  Assemble with /mx
  2. ;to ensure mixed case symbols.
  3.  
  4.   ifndef MSC
  5. MSC        equ    0
  6.   endif
  7.   ifndef TURBO
  8. TURBO        equ    0
  9.   endif
  10.   ifndef AZTEC
  11. AZTEC        equ    0
  12.   endif
  13.   if MSC + TURBO + AZTEC NE 1
  14.     .err One and only one of MSC, TURBO, and AZTEC should be defined.
  15.   endif
  16.  
  17.  
  18. ;Copyright, 1987, 1988, Russell Nelson
  19. ;Permission to use, copy, modify and distribute this software for any purpose
  20. ;and without fee is hereby granted, provided that the above copyright notice
  21. ;appears in all copies and that both that copyright notice and this permission
  22. ;appear in supporting documentation.  Russell Nelson MAKES NO REPRESENTATIONS
  23. ;ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  IT IS PROVIDED
  24. ;"AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
  25.  
  26. ;    .xlist
  27.  
  28. ;Note that symbols defined by these macros (pubproc, extproc, pubvar, extvar)
  29. ;should be referred by users of this package as 's@' where 's' is the defined
  30. ;symbol.  This hides the compiler's underscore convention.  For example,
  31. ;    include pmacros.h
  32. ;    extproc    foo
  33. ;    pubproc    bar
  34. ;    call    foo@
  35. ;    pend    bar
  36.  
  37. ;begin a public procedure and label it 'a'.  If 'd' is absent, then default
  38. ;to what LARGECODE says, otherwise 'd' must be 'far' or 'near'.
  39. ;Terminated by pend.
  40. pubproc    macro    a,d
  41.     cseg
  42.   if AZTEC
  43.     public    a&_
  44. a&@    equ    a&_
  45.   else
  46.     public    _&a
  47. a&@    equ    _&a
  48.   endif
  49.   ifb    <d>
  50.   ifdef LARGECODE
  51.   if AZTEC
  52. a&_    proc    far
  53.   else
  54. _&a    proc    far
  55.   endif
  56. first_param    =    6
  57.   else
  58.     if AZTEC
  59. a&_    proc    near
  60.     else
  61. _&a    proc    near
  62.     endif
  63. first_param    =    4
  64.   endif
  65.   else    ;ifb
  66.     if AZTEC
  67. a&_    proc    d
  68.     else
  69. _&a    proc    d
  70.     endif
  71.   ifidn    <d>,<far>
  72. first_param    =    6
  73.   else
  74. first_param    =    4
  75.   endif
  76.   endif
  77.     endm
  78.  
  79. ;used internally to expand first_param and increment it.
  80. _param    macro    a,o,t,n
  81. a    equ    t ptr o[bp]
  82. first_param = first_param + n
  83.     endm
  84.  
  85.  
  86. ;declare a parameter.  Use byte, word, ptr, or fptr (function pointer).
  87. param    macro    a,t
  88.   ifidn <t>,<byte>
  89.     _param    a,%first_param,byte,2
  90.   endif
  91.   ifidn <t>,<word>
  92.     _param    a,%first_param,word,2
  93.   endif
  94.   ifidn <t>,<ptr>
  95.   ifdef LARGEDATA
  96.     _param    a,%first_param,dword,4
  97.   else
  98.     _param    a,%first_param,word,2
  99.   endif
  100.   endif
  101.   ifidn <t>,<fptr>
  102.   ifdef LARGECODE
  103.     _param    a,%first_param,dword,4
  104.   else
  105.     _param    a,%first_param,word,2
  106.   endif
  107.   endif
  108.     endm
  109.  
  110. ;load a data pointer.
  111. ldptr    macro    off_reg,var,seg_reg
  112.   ifdef LARGEDATA
  113.     l&seg_reg    off_reg,var
  114.   else
  115.     mov    off_reg,var
  116.   endif
  117.     endm
  118.  
  119.  
  120. ;define a public procedure with parameters, like so:
  121. ;procdef    foo, <<bar, word>, <baz, ptr>>
  122. ;Terminated by pret, pend
  123. procdef    macro    n,a
  124.     pubproc    n
  125.     irp    one_param,<a>
  126.     param    one_param
  127.     endm
  128.     push    bp
  129.     mov    bp,sp
  130.     endm
  131.  
  132. ;return from a procedure with parameters.  Pairs with procdef
  133. pret    macro
  134.     pop    bp
  135.     ret
  136.     endm
  137.  
  138. ;end a public procedure.  Pairs with pubproc
  139. pend    macro    a
  140.   if AZTEC
  141. a&_    endp
  142.   else
  143. _&a    endp
  144.   endif
  145.     csegend
  146.     endm
  147.  
  148. ;define an external procedure called 'a'.  If 'd' is absent, then default
  149. ;to what LARGECODE says, otherwise 'd' must be 'far' or 'near'.
  150.  
  151. extproc    macro    a,d
  152.   ifb    <d>
  153.   ifdef LARGECODE
  154.     if AZTEC
  155.     extrn    a&_: far
  156.     else
  157.     extrn    _&a: far
  158.     endif
  159.   else
  160.     if AZTEC
  161.     extrn    a&_: near
  162.     else
  163.     extrn    _&a: near
  164.     endif
  165.   endif
  166.   else    ;ifb
  167.     if AZTEC
  168.     extrn    a&_: d
  169.     else
  170.     extrn    _&a: d
  171.     endif
  172.   endif
  173.   if AZTEC
  174. a&@    equ    a&_
  175.   else
  176. a&@    equ    _&a
  177.   endif
  178.     endm
  179.  
  180. ;open the code segment
  181. cseg    macro
  182.   if AZTEC
  183. codeseg    segment byte public 'code'
  184.     assume    cs:codeseg,ds:dataseg
  185.   endif
  186.   if TURBO
  187. _TEXT    segment byte public 'CODE'
  188. dgroup    group    _DATA
  189.     assume    cs:_TEXT,ds:dgroup
  190.   endif
  191.   if MSC
  192. _TEXT    SEGMENT WORD PUBLIC 'CODE'
  193.     assume    cs:_TEXT,ds:DGROUP
  194.   endif
  195.     endm
  196.  
  197. ;close the code segment
  198. csegend    macro
  199.   if AZTEC
  200. codeseg    ends
  201.   endif
  202.   if TURBO or MSC
  203. _TEXT    ends
  204.   endif
  205.     endm
  206.  
  207. ;assume that the ds is the code segment
  208. dscode    macro
  209.   if AZTEC
  210.     assume    ds:codeseg
  211.   endif
  212.   if TURBO or MSC
  213.     assume    ds:_TEXT
  214.   endif
  215.     endm
  216.  
  217.  
  218. ;Return the data segment
  219. getds    macro
  220.   if AZTEC
  221.     mov    ax,dataseg
  222.   endif
  223.   if TURBO
  224.     mov    ax,dgroup
  225.   endif
  226.   if MSC
  227.     mov    ax,DGROUP
  228.   endif
  229.     endm
  230.  
  231.  
  232. ;open the data segment
  233. dseg    macro
  234.   if AZTEC
  235. dataseg    segment word public 'data'
  236.   endif
  237.   if TURBO or MSC
  238. _DATA    segment word public 'DATA'
  239.   endif
  240.     endm
  241.  
  242. ;close the data segment
  243. dsegend    macro
  244.   if AZTEC
  245. dataseg    ends
  246.   endif
  247.   if TURBO or MSC
  248. _DATA    ends
  249.   endif
  250.     endm
  251.  
  252. ;define a public variable
  253. ;use it like so: pubvar a,<dw 0>
  254. pubvar    macro    n,d
  255.   if AZTEC
  256.     public    n&_
  257. n&_    d
  258. n&@    equ    n&_
  259.   endif
  260.   if TURBO or MSC
  261.     public    _&n
  262. _&n    d
  263. n&@    equ    _&n
  264.   endif
  265.     endm
  266.  
  267. ;define an external variable
  268. ;use it like so: extvar a,word
  269. ; or extvar a,byte
  270. extvar    macro    n,d
  271.   if AZTEC
  272.     extrn    n&_: d
  273. n&@    equ    n&_
  274.   endif
  275.   if TURBO or MSC
  276.     extrn    _&n: d
  277. n&@    equ    _&n
  278.   endif
  279.     endm
  280.  
  281. ; Conditional DS save/restore macros
  282. pushds    macro
  283.   ifdef LARGEDATA
  284.     push    ds
  285.   endif
  286.     endm
  287.  
  288. popds    macro
  289.   ifdef LARGEDATA
  290.     pop    ds
  291.   endif
  292.     endm
  293.  
  294. ; Conditional ES save/restore macros
  295. pushes    macro
  296.   ifdef LARGEDATA
  297.     push    es
  298.   endif
  299.     endm
  300.  
  301. popes    macro
  302.   ifdef LARGEDATA
  303.     pop    es
  304.   endif
  305.     endm
  306.  
  307. ;define the segments so that they exist.
  308. dseg
  309. dsegend
  310.   if MSC
  311. CONST    SEGMENT WORD PUBLIC 'CONST'
  312. CONST    ENDS
  313. _BSS    SEGMENT WORD PUBLIC 'BSS'
  314. _BSS    ENDS
  315. DGROUP    GROUP    CONST,_BSS,_DATA
  316.   endif
  317. cseg
  318. csegend
  319.  
  320. ;define an interrupt handler.
  321. inthandler    macro    xxxname,xxxnum
  322.  
  323.     dseg
  324.     extvar    sssave,word
  325.     extvar    spsave,word
  326.     extvar    intstk,byte
  327.     dsegend
  328.  
  329.     extproc    doret
  330.     extproc    xxxname&int
  331.  
  332.     pubproc    xxxname&xxxnum&vec
  333.  
  334.     cli            ; this code is not re-entrant, so make sure it
  335.                 ; is not interrupted. some multi-taskers
  336.                 ; intercept interrupt handlers, so be careful!
  337.  
  338.     push    ax        ; save ax first.
  339.     push    ds        ; save on user stack
  340.     getds
  341.     mov    ds,ax
  342.  
  343.     mov    sssave@,ss    ; stash user stack context
  344.     mov    spsave@,sp
  345.  
  346.     mov    ss,ax        ; set up interrupt stack
  347.     lea    sp,intstk@
  348.  
  349.     push    bx        ; save user regs on interrupt stack
  350.     push    cx
  351.     push    dx
  352.     push    bp
  353.     push    si
  354.     push    di
  355.     push    es
  356.  
  357.   ifndef LARGEDATA
  358.     mov    es,ax        ; small data assumes ES == DS
  359.   endif
  360.  
  361.   if TURBO or MSC
  362.     cld            ; in case "movsb" or "movsw" is used
  363.   endif
  364.  
  365.     mov    ax,xxxnum    ; arg for service routine
  366.     push    ax
  367.     call    xxxname&int@
  368.     pop    ax
  369.     jmp    doret@
  370.     pend    xxxname&xxxnum&vec
  371.  
  372.     endm
  373.  
  374.     assume    cs:nothing,ds:nothing,es:nothing,ss:nothing
  375.  
  376. ;    .list
  377.